package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.tulele.vtool.model.UserWithdrawLogInfo;
import com.tulele.vtool.service.IUserInfoService;
import com.tulele.vtool.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class EnterprisePayTask {

    @Autowired
    private WxPayService wxService;

    @Autowired
    private IUserInfoService userInfoService;

    @Async
    public void entPay(Integer userWithdrawId, EntPayRequest request) {

        log.info("[enter entPay] = " + userWithdrawId);

        EntPayResult entPayResult = null;
        try {
            entPayResult = wxService.getEntPayService().entPay(request);
        } catch (WxPayException e) {
            e.printStackTrace();
        }

        log.info("[EntPayResult] = " + JSON.toJSONString(entPayResult));

        UserWithdrawLogInfo userWithdrawLogInfo = new UserWithdrawLogInfo();
        userWithdrawLogInfo.setId(userWithdrawId);
        userWithdrawLogInfo.setOrderId(entPayResult.getPaymentNo());
        userWithdrawLogInfo.setProcessStatus(1);
        userWithdrawLogInfo.setModifyTime(DateUtil.getCurDate());

        userInfoService.modifyWithdrawInfo(userWithdrawLogInfo);

        log.info("[exit entPay] = " + userWithdrawId);

    }

}
